import { axios } from '@/utils/request'
const api = {
  dictData: '/system/dict/data',
  tableDictData: '/system/tableDict'
}

// 字典两种用法，各有优缺点
// 1.Map 需要数组的时候构造数组不方便
// operTypeMap = await getDictMap('sys_oper_type')
// this.operTypeMap = operTypeMap
// this.operTypeMap.forEach((value, key, mymap) => {
//   this.businessTypes.push({ code: key, label: value })
// })
// 2.数组 在modal调用没有map方便
// this.businessTypes = await getDictArray('sys_oper_type')
// this.businessTypes.map(d => {
//   operTypeMap[d.dictValue] = { text: d.dictLabel }
// })
// this.operTypeMap = operTypeMap

/**
 * 根据类型获取字典内容 返回List
 * @param {String} dictType 字典类型
 */
export function tableDictList (code) {
  // return axios({
  //   url: api.dictData + '/type/'+dictType,
  //   method: 'get'
  // })
  const tableCode=code;
  return new Promise((resolve, reject) => {
    console.info("send msg:"+api.tableDictData + '/data/' + tableCode)
    // ajax
    axios({
      url: api.tableDictData + '/data/' + tableCode,
      method: 'get'
    }).then(res => {
      console.info("dictres:"+JSON.stringify(res))
      var list = []
      for (var i = 0; i < res.length; i++) {
        var key = res[i].key
        var value = res[i].value
//        if (key != '' && !isNaN(key)) {
//          var json = { 'key': key+"", 'value': value }
//          list.push(json)
//        } else {
          var obj = { 'key': key, 'value': value }
          list.push(obj)
 //       }
      }
      resolve(list)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 根据类型获取字典内容 返回Map
 * @param {String} dictType 字典类型
 */
export function getTableDictKey (code) {
  return new Promise((resolve, reject) => {
  // ajax
    axios({
      url: api.tableDictData + '/data/' + code,
      method: 'get'
    }).then(res => {
      console.info(res.length + 'res:' + res)
      const map = new Map()

      console.info('length:' + res.length)
      for (var i = 0; i < res.length; i++) {
        var d = res[i]
        map[d.key] = d.value
      }

      console.info('sys_common_status:' + JSON.stringify(map))
      resolve(map)
    }).catch(err => {
      reject(err)
    })
  })
}


export function dictList (dictType) {
  // return axios({
  //   url: api.dictData + '/type/'+dictType,
  //   method: 'get'
  // })

  return new Promise((resolve, reject) => {
    // ajax
    axios({
      url: api.dictData + '/type/' + dictType,
      method: 'get'
    }).then(res => {
      var list = []
      for (var i = 0; i < res.length; i++) {
        var value = res[i].dictValue
        var name = res[i].dictLabel
       // value = value.replace(/\ /g, '')
//        if (value != '' && !isNaN(value)) {
//          var json = { 'dictValue': value * 1, 'dictLabel': name }
//          list.push(json)
//        } else {
          var obj = { 'dictValue': value, 'dictLabel': name }
          list.push(obj)
//        }
      }
      console.info(dictType + ':sys_common_status:' + JSON.stringify(list))
      resolve(list)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 根据类型获取字典内容 返回Map
 * @param {String} dictType 字典类型
 */
export function getDictMapKey (dictType) {
  return new Promise((resolve, reject) => {
  // ajax
    axios({
      url: api.dictData + '/type/' + dictType,
      method: 'get'
    }).then(res => {
      console.info(res.length + 'res:' + res)
      const map = new Map()

      console.info('length:' + res.length)
      for (var i = 0; i < res.length; i++) {
        var d = res[i]
        map[d.dictValue] = d.dictLabel
      }

      console.info('sys_common_status:' + JSON.stringify(map))
      resolve(map)
    }).catch(err => {
      reject(err)
    })
  })
}
/**
 * 根据类型获取字典内容 返回数组
 * @param {String} dictType 字典类型
 */
export function getDictArray (dictType) {
  return axios({
    url: api.dictData + '/type/' + dictType,
    method: 'get'
  })
}
